<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>角色管理</title>
<script src="js/jquery-1.9.1.min.js"></script>

<script src="js/jquery.mCustomScrollbar.concat.min.js"></script>
<link href="css/jquery.mCustomScrollbar.css" rel="stylesheet" type="text/css">

<script src="js/jquery.jtip.js"></script>
<link href="css/jquery.jtip.css" rel="stylesheet" type="text/css">

<script src="js/rb.js"></script>
<link href="css/rb.css" rel="stylesheet" type="text/css">

<script src="js/dump.js"></script>
<script>
$(document).ready(function() {
	$(".rb_mainframe_middle").css("width",FRAMEWIDTH-20+"px");
	$(".rb_mainframe_middle_content").mCustomScrollbar({
    	theme:"dark",
		advanced:{updateOnContentResize:true}
	});
	//$(".rb_mainframe_middle_content").css("height",$(".rb_mainframe_middle").height()-40+"px");
	
	//获取列表	
	getRoleList();
	
	/*	
	$("#showall_btn").click(function(){
		if ($(this).text()=="展开列表"){
			$(this).text("收缩列表");
		}else{
			$(this).text("展开列表");
		}
		
		$("#report tr.odd").each(function(){
			$(this).next("tr").toggle();		
			$(this).find(".arrow").toggleClass("up");
			$(this).find("td").toggleClass("up");
			$(this).find("td").not(":first-child").toggleClass("color");
    	});
	});	
	*/
	
});
//为新创建的TR添加事件
function initTR(trid){
	$("#"+trid).addClass("odd");
	$("#"+trid).next("tr").hide();
	$("#"+trid).show();
	
	$("#"+trid).click(function(){
		$(this).next("tr").toggle(200);
		$(this).find(".arrow").toggleClass("up");
		$(this).find("td").toggleClass("up");
		$(this).find("td").not(":first-child").toggleClass("color");
		getRoleMember($(this).attr("id"));
	});
}
//获得角色列表
function getRoleList(){
	$.ajax({
		timeout:2000, 
		type: AJAXMETHOD,
        async: false,
        url: SERVER+"role",
        dataType: AJAXJSON,
        jsonp: "callback",
        jsonpCallback:"callback",
        success: function(json){
			for (var i=0;i<json.respBody.data.length;i++){
				$("#report").append("<tr id='"+json.respBody.data[i].id+"'><td>"+json.respBody.data[i].name+"</td><td>"+json.respBody.data[i].description+"</td><td><a href=\"javascript:JT_editRoleName('item_"+json.respBody.data[i].id+"','修改角色信息','"+json.respBody.data[i].id+"')\" id='item_"+json.respBody.data[i].id+"'>修改</a> <a href=\"javascript:alertRoleDel('"+json.respBody.data[i].id+"');\">删除</a></td><td><div class='arrow'></div></td></tr><tr><td colspan='4'>该角色下没有对应人员</td></tr>");			
			}
			
			//底部占位
			$("#report").append("<tr><td height='150px;'></td></tr>");
			$("#report tr:odd").addClass("odd");
			$("#report tr:not(.odd)").hide();
			$("#report tr:first-child").show();
			
			$("#report tr.odd").click(function(){
				$(this).next("tr").toggle(200);
				$(this).find(".arrow").toggleClass("up");
				$(this).find("td").toggleClass("up");
				$(this).find("td").not(":first-child").toggleClass("color");
				//getRoleMember($(this).next("tr").find("td"));
				getRoleMember($(this).attr("id"));
			});
        },
        error: function(){
            
    	}
	});	
}
//获取角色下人员
function getRoleMember(tdid){
	$.ajax({
		timeout:2000, 
		type: AJAXMETHOD,
        async: false,
        url: SERVER+"role/getUsers/"+tdid,
        dataType: AJAXJSON,
        jsonp: "callback",
        jsonpCallback:"callback",
        success: function(json){
			var str = "<ul>";
			if (json.respList.length>0){
				for (var i=0;i<json.respList.length;i++){
					str += "<li><img class='report_tx' src='assets/temp_tx1.jpg'/><div>"+json.respList[i].realName+"</div></li>";		
				}
			}else{
				str += "该角色下没有对应人员";
			}
			str += "</ul>";
			$("#"+tdid).next("tr").find("td").html(str);
        },
        error: function(){
            
    	}
	});		
}
//创建角色
function createRole(){
	var roleName = $("#createRoleName").val();
	var roleEtc = $("#createRoleEtc").val();
	if (roleName.length>0){
		$.ajax({
			timeout:2000, 
			type: AJAXMETHOD,
			async: false,
			url: SERVER+"role/add/role?isValid=1&isFlow=1&name="+roleName+"&description="+roleEtc,
			dataType: AJAXJSON,
			jsonp: "callback",
			jsonpCallback:"callback",
			success: function(json){
				RB.Toast("创建<b>\""+roleName+"\"</b>角色成功！");
				jt_close();
				$("#report tr:first-child").after("<tr id='"+json.respBody.id+"'><td>"+roleName+"</td><td>"+roleEtc+"</td><td><a href=\"javascript:JT_editRoleName('"+json.respBody.id+"_1','修改角色信息','"+json.respBody.id+"')\" id='"+json.respBody.id+"_1'>修改</a> <a href=\"javascript:alertRoleDel('"+json.respBody.id+"');\">删除</a></td><td><div class='arrow'></div></td></tr><tr><td colspan='4'>该角色下没有对应人员</td></tr>");
				initTR(json.respBody.id);
			},
			error: function(){
				alert("CreateRole Error!");
			}
		});
	}else{
		RB.Toast("角色名不能为空！");
	}
		
}
//修改角色名执行
function editRole(roleId){
	var roleName = $("#editRoleName").val();
	var roleEtc = $("#editRoleEtc").val();
	//这里执行持久化操作
	if(roleName.length<=0){
		RB.Toast("角色名不能为空！");
	}else{
		$.ajax({
			timeout:2000, 
			type: AJAXMETHOD,
			async: false,
			url: SERVER+"role/add/role?id="+roleId+"&isValid=1&isFlow=1&name="+roleName+"&description="+roleEtc,
			dataType: AJAXJSON,
			jsonp: "callback",
			jsonpCallback:"callback",
			success: function(json){
				$("#"+roleId+" td:first-child").text(roleName);
				$("#"+roleId+" td:nth-child(2)").text(roleEtc);
				RB.Toast("修改角色信息成功！");
				jt_close();
			},
			error: function(){
				alert("EditRole Error!");
			}
		});
	}
	
}
//删除角色询问
function alertRoleDel(id){
	var rolename = $("#"+id+" td:first-child").text();
	$.ajax({
		timeout:2000, 
		type: AJAXMETHOD,
        async: false,
        url: SERVER+"role/getUsers/"+id,
        dataType: AJAXJSON,
        jsonp: "callback",
        jsonpCallback:"callback",
        success: function(json){
			if (json.respList.length>0){
				RB.AlertInfo("系统提示","<b>\""+rolename+"\"</b>角色下有用户存在，禁止删除该角色!");	
			}else{
				RB.Alert("系统提示","你确定要删除<b>\""+rolename+"\"</b>角色吗？","roleDel('"+id+"','"+rolename+"')");
			}
        },
        error: function(){
            
    	}
	});	
}
//删除角色执行
function roleDel(id,rolename){
	$.ajax({
		timeout:2000, 
		type: AJAXMETHOD,
        async: false,
        url: SERVER+"role/delete/"+id,
        dataType: AJAXJSON,
        jsonp: "callback",
        jsonpCallback:"callback",
        success: function(json){			
			RB.Toast("删除<b>\""+rolename+"\"</b>角色成功！");
			
			$("#"+id).next("tr").remove();
			$("#"+id).remove();
        },
        error: function(){
            
    	}
	});	
	
}
</script>
<style type="text/css">
a{
	color 				: #ff0000;	
	text-decoration		: none;
}

a:hover{
	text-decoration		: underline;
}
#report { border-collapse:collapse;}
#report h4 { margin:0px; padding:0px;}
#report ul { margin:10px 0 10px 10px; padding:0px;}
#report ul li {
	float:left;
	width:30%;
	height:40px;
	list-style-type:none;
	padding:10px;
	font-size:13px;
}
#report ul li div {
	float:left;
	height:40px;
	line-height:40px;
	margin-left:10px;
	
}
#report ul li div:nth-child(2){
	font-weight:bold;
	width:50%;
}
#report table{ width:100%;}
#report th { background:#6e8187; color:#fff; padding:7px 15px; text-align:left;width:42%;height:30px;}
#report td { background:#fbe7ce none repeat-x scroll center left; color:#000; padding:20px 10px;color:#5a3c44; }
#report tr.odd td { background:#fff; cursor:pointer;color:#837076; }
#report tr.odd td.up { background:#fbe7ce; cursor:pointer; font-weight:bold; border-top:#FFF 1px solid; color:#5a3c44; }
#report tr.odd td.color { color:#fbe7ce;}
#report div.arrow { background:transparent url(assets/arrows.png) no-repeat scroll 0px -16px; width:16px; height:16px; display:block;}
#report div.up { background-position:0px 0px;}
.report_tx{
	width:40px;
	height:40px;
	background-size:40px 40px;
	float:left;
	border-radius:20px;
}
.rb_mainframe_middle_title{
	background-color:#6e8187;
}
</style>
</head>

<body onselectstart="return false"> 
<div class="rb_mainframe_top">
	<div class="rb_mainframe_top_title_first">
    	组织结构
    </div>
    <div class="rb_mainframe_top_title_second">
    	角色管理
    </div>    
    <div class="rb_mainframe_top_btn">帮助</div>   
  	<div class="rb_mainframe_top_btn"><a href="javascript:JT_createRole('createRoleShow','新建角色');" id="createRoleShow">新建角色</a></div>
    <!--    
    <div class="rb_mainframe_top_input"><img src="assets/top_input_icon_search.png"/><input type="text" placeholder="输入角色名..." name="searchDep" id="searchDep" /><input name="sumbit" type="button" value="搜索" onClick="RB.AlertInfo('系统提示','你点击了一个按钮，点击确认触发事件，点击取消关闭Alert!');"></div>
    -->

</div>

<div class="rb_mainframe_content">

    <div class="rb_mainframe_middle">
        <div class="rb_mainframe_middle_content">
            <table id="report">
                <tr>
                    <th>角色名称</th>
                    <th>说明</th>
                    <th>操作</th>
                    <th></th>
                </tr>       
            </table>
        </div>
        <!--
        <div class="rb_mainframe_middle_page">
        	<div class="rb_mainframe_middle_bottom_btn_left">首页</div>
            <div class="rb_mainframe_middle_bottom_btn_left">上一页</div>
            <div class="rb_mainframe_middle_bottom_btn_left">下一页</div>
            <div class="rb_mainframe_middle_bottom_btn_left">尾页</div>
            <div class="rb_mainframe_middle_bottom_btn_right showall_btn" id="showall_btn">展开列表</div>
        </div>
        -->
    </div>
</div>
</body>
</html>
